/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include <stdio.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint8_t seg_map[10] = {0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xE0,0xFE,0xF6};
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

int ScanKeyState(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin){
	if (HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == 0){
	  HAL_Delay(10);
	  if (HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == 0){
		  return 1;
	  } else{
		  return 2;
	  }
	}

	return 0;
}

int displayNumber(uint32_t num){
	uint8_t buf[8];
	uint8_t seg_map[10] = {0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xE0,0xFE,0xF6};
	int idx;
	int ret;

	idx = (num%10000)/1000;
	buf[0] = (uint8_t)seg_map[idx];//4--
	idx = (num%1000)/100;
	buf[1] = (uint8_t)seg_map[idx];

	idx = (num%100)/10;
	buf[2] = (uint8_t)seg_map[idx];

	idx = num%10;
	buf[3] = (uint8_t)seg_map[idx];
	idx = 0;
	buf[4] = (uint8_t)0xFF;
	buf[5] = (uint8_t)0xFF;
	buf[5] = (uint8_t)0xFF;
	buf[7] = (uint8_t)0xFF;

	ret = HAL_I2C_Mem_Write(&hi2c1,0x70, 0x10, 1, buf,8, 100);
	if (ret == 0){
		return 0;
	}

	return -1;
}

void BEEP_Stop(void){
	HAL_TIM_PWM_Stop(&htim4,TIM_CHANNEL_1);
}

void BEEP_Start(void)
{
	uint32_t Period = 500;
	uint32_t Percent = 50;

	htim4.Init.Period = Period;

	HAL_TIM_PWM_Init(&htim4);

	//Period = htim4.Init.Period;
	if(HAL_TIM_Base_Start_IT(&htim4) != HAL_OK)
	{
		Error_Handler();
	}
	HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);

	//设置指定占空比
	__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1, (Period * Percent) / 100);
}

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */
	uint32_t ticks_now;
	uint32_t ticks_sec;
	uint8_t key_mask = 0x00;
	int is_timer_on;
	uint32_t timer_left = 0;

	int ret;

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  if(IS_ENGINEERING_BOOT_MODE())
  {
    /* Configure the system clock */
    SystemClock_Config();
  }

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_TIM4_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  is_timer_on = 0;
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  ticks_now = HAL_GetTick();
	  ticks_sec = ticks_now/1000;

	  if ((key_mask & 0x01) == 0){
		  ret = ScanKeyState(GPIOE, GPIO_PIN_9);
		  if (ret == 1){
			  key_mask |= 0x01;
			  //按下事件key3
			  is_timer_on = 1;
		  } else{
			  key_mask &= ~0x01;
		  }
	  }

	  if ((key_mask & 0x02) == 0){
		  ret = ScanKeyState(GPIOG, GPIO_PIN_2);
		  if (ret == 1){
			  key_mask |= 0x02;
			  //按下事件key4
			  is_timer_on = 0;
			  BEEP_Stop();
		  } else{
			  key_mask &= ~0x02;
		  }
	  }

	  if ((key_mask & 0x04) == 0){
		  ret = ScanKeyState(GPIOG, GPIO_PIN_1);
		  if (ret == 1){
			  key_mask |= 0x04;
			  //按下事件key5
			  is_timer_on = 0;
			  timer_left++;
		  } else{
			  key_mask &= ~0x04;
		  }
	  }

	  if ((key_mask & 0x08) == 0){
		  ret = ScanKeyState(GPIOG, GPIO_PIN_0);
		  if (ret == 1){
			  key_mask |= 0x08;
			  //按下事件key6
			  is_timer_on = 0;
			  if (timer_left > 0){
				  timer_left--;
			  }
		  } else{
			  key_mask &= ~0x08;
		  }
	  }
	  ticks_now = HAL_GetTick();
	  if (ticks_sec != ticks_now/1000){
		 //说明这是一个新的毫秒，
		  ticks_sec = ticks_now/1000;
		  if (is_timer_on && (timer_left > 0)){
			  timer_left--;
			  if (timer_left == 0){
				  //蜂鸣器鸣叫
				  BEEP_Start();
			  }
		  }

		  displayNumber(timer_left);
	  }
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDivValue = RCC_HSI_DIV1;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL4.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** RCC Clock Config
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_ACLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_PCLK3|RCC_CLOCKTYPE_PCLK4
                              |RCC_CLOCKTYPE_PCLK5;
  RCC_ClkInitStruct.AXISSInit.AXI_Clock = RCC_AXISSOURCE_HSI;
  RCC_ClkInitStruct.AXISSInit.AXI_Div = RCC_AXI_DIV1;
  RCC_ClkInitStruct.MCUInit.MCU_Clock = RCC_MCUSSOURCE_HSI;
  RCC_ClkInitStruct.MCUInit.MCU_Div = RCC_MCU_DIV1;
  RCC_ClkInitStruct.APB4_Div = RCC_APB4_DIV1;
  RCC_ClkInitStruct.APB5_Div = RCC_APB5_DIV1;
  RCC_ClkInitStruct.APB1_Div = RCC_APB1_DIV1;
  RCC_ClkInitStruct.APB2_Div = RCC_APB2_DIV1;
  RCC_ClkInitStruct.APB3_Div = RCC_APB3_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
